<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

            <el-form-item label="网关" prop="gatewayId">
                <gateway-select @changeGateway="changeGateway"/>
            </el-form-item>

            <el-form-item label="操作类型" prop="operationType">
                <el-select v-model="queryParams.operationType"
                           placeholder="请选择操作类型"
                           size="small"
                           @change="changeOperationType"
                           clearable
                >
                    <el-option
                        v-for="item in operationTypeList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                </el-select>
            </el-form-item>


            <el-form-item label="操作状态" prop="operationStatus">
                <el-select v-model="queryParams.operationStatus"
                           placeholder="请选择操作状态"
                           size="small"
                           @change="changeOperationStatus"
                           clearable
                >
                    <el-option
                        v-for="item in operationStatusList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                </el-select>
            </el-form-item>

            <el-form-item label="指令类型" prop="commandType">
                <el-select v-model="queryParams.commandType"
                           placeholder="请选择指令类型"
                           size="small"
                           @change="changeCommandType"
                           clearable
                >
                    <el-option
                        v-for="item in commandTypeList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                </el-select>
            </el-form-item>

            <el-form-item label="具体名称" prop="operationName">
                <el-input v-model="queryParams.operationName" placeholder="请输入名称" clearable size="small"/>
            </el-form-item>


            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>

        </el-form>

        <!--    <el-row :gutter="10" class="mb8">-->
        <!--      <el-col :span="1.5">-->
        <!--        <el-button-->
        <!--          type="primary"-->
        <!--          plain-->
        <!--          icon="el-icon-plus"-->
        <!--          size="mini"-->
        <!--          @click="handleAdd"-->
        <!--          v-hasPermi="['business:log:add']"-->
        <!--        >新增</el-button>-->
        <!--      </el-col>-->
        <!--      <el-col :span="1.5">-->
        <!--        <el-button-->
        <!--          type="success"-->
        <!--          plain-->
        <!--          icon="el-icon-edit"-->
        <!--          size="mini"-->
        <!--          :disabled="single"-->
        <!--          @click="handleUpdate"-->
        <!--          v-hasPermi="['business:log:edit']"-->
        <!--        >修改</el-button>-->
        <!--      </el-col>-->
        <!--      <el-col :span="1.5">-->
        <!--        <el-button-->
        <!--          type="danger"-->
        <!--          plain-->
        <!--          icon="el-icon-delete"-->
        <!--          size="mini"-->
        <!--          :disabled="multiple"-->
        <!--          @click="handleDelete"-->
        <!--          v-hasPermi="['business:log:remove']"-->
        <!--        >删除</el-button>-->
        <!--      </el-col>-->
        <!--      <el-col :span="1.5">-->
        <!--        <el-button-->
        <!--          type="warning"-->
        <!--          plain-->
        <!--          icon="el-icon-download"-->
        <!--          size="mini"-->
        <!--          @click="handleExport"-->
        <!--          v-hasPermi="['business:log:export']"-->
        <!--        >导出</el-button>-->
        <!--      </el-col>-->
        <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
        <!--    </el-row>-->

        <el-table v-loading="loading" :data="logList" ref="table" @selection-change="handleSelectionChange">

            <el-table-column label="序号" align="center" type="index" :index="calculateIndex"/>

            <el-table-column label="项目" align="center" prop="projectName"/>
            <el-table-column label="网关" align="center" prop="gatewayName"/>
            <el-table-column label="操作类型" align="center" prop="operationType">
                <template slot-scope="scope">
                    <span v-if="scope.row.operationType ==='0'">读</span>
                    <span v-if="scope.row.operationType ==='1'">写</span>
                </template>
            </el-table-column>

            <el-table-column label="操作描述" align="center" prop="operationAction"/>
            <el-table-column label="具体名称" align="center" prop="operationName"/>
            <el-table-column label="具体数值" align="center" prop="operationValue"/>
            <el-table-column label="操作状态" align="center" prop="operationStatus">
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.operationStatus ==='0'" type="danger" disable-transitions>
                        失败
                    </el-tag>
                    <el-tag v-if="scope.row.operationStatus ==='1'" type="success" disable-transitions>
                        成功
                    </el-tag>
                    <el-tag v-if="scope.row.operationStatus ==='2'" type="danger" disable-transitions>
                        超时
                    </el-tag>
                </template>
            </el-table-column>

            <el-table-column label="耗时(ms)" align="center" prop="useTime"/>
            <el-table-column label="指令类型" align="center" prop="commandType">
                <template slot-scope="scope">
                    <span v-if="scope.row.commandType ==='0'">定时</span>
                    <span v-if="scope.row.commandType ==='1'">手动</span>
                </template>
            </el-table-column>
            <el-table-column label="操作时间" align="center" prop="createTime">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.createTime) }}</span>
                </template>
            </el-table-column>

            <el-table-column label="操作人" align="center" prop="createBy"/>

            <!--      <el-table-column label="${comment}" align="center" prop="projectId" />-->
            <!--      <el-table-column label="${comment}" align="center" prop="gatewayId" />-->
            <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
            <!--        <template slot-scope="scope">-->
            <!--          <el-button-->
            <!--            size="mini"-->
            <!--            type="text"-->
            <!--            icon="el-icon-edit"-->
            <!--            @click="handleUpdate(scope.row)"-->
            <!--            v-hasPermi="['business:log:edit']"-->
            <!--          >修改</el-button>-->
            <!--          <el-button-->
            <!--            size="mini"-->
            <!--            type="text"-->
            <!--            icon="el-icon-delete"-->
            <!--            @click="handleDelete(scope.row)"-->
            <!--            v-hasPermi="['business:log:remove']"-->
            <!--          >删除</el-button>-->
            <!--        </template>-->
            <!--      </el-table-column>-->

        </el-table>

        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />

        <!-- 添加或修改操作记录对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="具体名称" prop="operationName">
                    <el-input v-model="form.operationName" placeholder="请输入具体名称"/>
                </el-form-item>
                <el-form-item label="${comment}" prop="projectId">
                    <el-input v-model="form.projectId" placeholder="请输入${comment}"/>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
import { listLog, getLog, delLog, addLog, updateLog } from '@/api/business/operationLog'
import gatewaySelect from '../../common/GatewaySelect.vue'

export default {
    name: 'operationLog',
    components: {
        gatewaySelect
    },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 操作记录表格数据
            logList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                operationAction: null,
                operationType: null,
                operationName: null,
                operationStatus: null,
                gatewayId: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {},
            operationTypeList: [
                { value: '0', label: '读' },
                { value: '1', label: '写' }
            ],
            operationStatusList: [
                { value: '0', label: '失败' },
                { value: '1', label: '成功' },
                { value: '2', label: '超时' }
            ],
            commandTypeList: [
                { value: '0', label: '定时' },
                { value: '1', label: '手动' }
            ],
            // tableHeight: 300
        }
    },
    created() {
        // this.$nextTick(() => {
        //   this.setTableHeight()
        //   // 监听窗口大小变化
        //   window.onresize = () => {
        //     this.setTableHeight()
        //   }
        // })
    },
    beforeDestroy() {
        // 由于window.onresize是全局事件，在其他页面改变界面时也会执行，这样可能会出现问题，需要在离开这个界面时注销window.onresize事件
        window.onresize = null
    },
    methods: {

        // setTableHeight () {
        //   this.tableHeight = window.innerHeight - this.$refs.table.$el.offsetTop - 115
        //   console.log('this.tableHeight')
        //   console.log(this.tableHeight)
        // },

        changeOperationType(val) {
            this.getList()
        },

        changeOperationStatus(val) {
            this.getList()
        },

        changeCommandType(val) {
            this.getList()
        },

        // 接收子组件传递的消息
        changeGateway(obj) {
            this.queryParams.gatewayId = obj.gatewayId
            this.getList()
        },

        calculateIndex(index) {
            const pageNum = this.queryParams.pageNum
            const pageSize = this.queryParams.pageSize
            return index + 1 + (pageNum - 1) * pageSize
        },

        getList() {
            this.loading = true
            listLog(this.queryParams).then(response => {
                this.logList = response.rows
                this.total = response.total
                this.loading = false
            })
        },

        cancel() {
            this.open = false
            this.reset()
        },

        reset() {
            this.form = {
                operationId: null,
                operationAction: null,
                operationType: null,
                operationName: null,
                operationStatus: null,
                projectId: null,
                gatewayId: null,
                createTime: null,
                createBy: null
            }
            this.resetForm('form')
        },

        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },

        resetQuery() {
            this.resetForm('queryForm')
            this.handleQuery()
        },

        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.operationId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },

        handleAdd() {
            this.reset()
            this.open = true
            this.title = '添加操作记录'
        },

        handleUpdate(row) {
            this.reset()
            const operationId = row.operationId || this.ids
            getLog(operationId).then(response => {
                this.form = response.data
                this.open = true
                this.title = '修改操作记录'
            })
        },

        submitForm() {
            this.$refs['form'].validate(valid => {
                if (valid) {
                    if (this.form.operationId != null) {
                        updateLog(this.form).then(response => {
                            this.$modal.msgSuccess('修改成功')
                            this.open = false
                            this.getList()
                        })
                    } else {
                        addLog(this.form).then(response => {
                            this.$modal.msgSuccess('新增成功')
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },

        handleDelete(row) {
            const operationIds = row.operationId || this.ids
            this.$modal.confirm('是否确认删除操作记录编号为"' + operationIds + '"的数据项？').then(function() {
                return delLog(operationIds)
            }).then(() => {
                this.getList()
                this.$modal.msgSuccess('删除成功')
            }).catch(() => {
            })
        },

        handleExport() {
            this.download('business/log/export', {
                ...this.queryParams
            }, `log_${new Date().getTime()}.xlsx`)
        }

    }
}
</script>
<style>
.el-table__body-wrapper.is-scrolling-none {
    overflow-y: auto;
    max-height: 700px;
}
</style>
